<template>
  <el-card class="box-card">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="通过密码修改" name="1">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名">
            <el-input v-model="ruleForm.displayName" disabled></el-input>
          </el-form-item>
          <el-form-item label="账号">
            <el-input v-model="ruleForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="当前密码" prop="nowPassWord">
            <el-input v-model="ruleForm.nowPassWord"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="ruleForm.confirmPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="通过身份证号码修改" name="2">
        <el-form :model="idCardForm" status-icon ref="idCardForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名">
            <el-input v-model="idCardForm.displayName" disabled></el-input>
          </el-form-item>
          <el-form-item label="账号">
            <el-input v-model="idCardForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="身份证号码" prop="idcard">
            <el-input v-model="idCardForm.idcard" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input type="password" v-model="idCardForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="idCardForm.confirmPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('idCardForm')">提交</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>

  </el-card>
</template>
<script>
import api from "@/api/dispose";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        displayName: "",
        username: "",
        nowPassWord: '',
        password: '',
        checkPass: '',
      },
      activeName: '1',
      idCardForm: {
        displayName: '',
        username: '',
        idcard: '',
        password: '',
        checkPass: '',
      },
      rules: {
        nowPassWord: [
          { validator: validatePass3, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      },
    }
  },
  mounted() {

    api.getPersonalData().then((res) => {
      if (res.code == 0) {
        this.ruleForm = res.data;
        this.ruleForm.gender = this.ruleForm.gender + "";
      }
    });
  },
  methods: {
    handleClick(tab, event) {
      if (tab.name == '2') {
        this.idCardForm.displayName = this.ruleForm.displayName
        this.idCardForm.username = this.ruleForm.username
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = {
            password: this.ruleForm.password,
            oldPassword: this.ruleForm.nowPassWord,
            confirmPassword: this.ruleForm.confirmPassword,
            displayName: this.ruleForm.displayName,
            id: this.ruleForm.id,
            status: this.ruleForm.status,
            switch_status: false,
            username: this.ruleForm.username
          }
          api.updatePasword(data).then((res) => {
            if (res.code == 0) {
              this.$message.success('操作成功');
            }
          }).catch(error => {
            this.$message.warning('操作异常,请联系管理员');
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  /* width: 480px; */
}

.first-div {
  background-color: white;
}
</style>
